package com.project.myproject.config;


import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.connection.balancer.RandomLoadBalancer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
public class RedisConfig {

    private static String clusterNodes = "redis://10.86.49.10:6379,redis://10.86.49.11:6379,redis://10.86.49.12:6379";

    private static String password = null;

    private static Integer masterconnectionpoolsize = 10;

    private static Integer slaveconnectionpoolsize = 10;

    private static Integer masterconnectionminimumidlesize = 64;

    private static Integer slaveconnectionminimumidlesize = 64;

    private static Integer scanInterval = 2000;


    @Bean
    public RedissonClient getRedissionClient() {

        String[] cNodes = clusterNodes.split(",");
        List<String> list = new ArrayList<>();
        //分割出集群节点
        list.addAll(Arrays.asList(cNodes));
        String[] reids = list.toArray(new String[list.size()]);

        Config config = new Config();
        config.useClusterServers()
                .setScanInterval(scanInterval) // 集群状态扫描间隔时间，单位是毫秒
                .setLoadBalancer(new RandomLoadBalancer()) //随机调度算法
                //可以用"rediss://"来启用SSL连接
                .addNodeAddress(reids)
                .setMasterConnectionPoolSize(64)
                .setSlaveConnectionPoolSize(64)
                .setMasterConnectionMinimumIdleSize(10)
                .setSlaveConnectionMinimumIdleSize(10);
        if (password != null) {
            config.useClusterServers().setPassword(password);
        }
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }
}
